<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>sha2.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_crypt_sha2.js.html">sha2.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Base class for SHA-2 cryptographic hash.
<a name="line17"></a> *
<a name="line18"></a> * Variable names follow the notation in FIPS PUB 180-3:
<a name="line19"></a> * http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf.
<a name="line20"></a> *
<a name="line21"></a> * To implement specific SHA-2 such as SHA-256, create a sub-class with
<a name="line22"></a> * overridded reset(). See sha256.js for an example.
<a name="line23"></a> *
<a name="line24"></a> * Some code similar to SHA1 are borrowed from sha1.js written by mschilder@.
<a name="line25"></a> *
<a name="line26"></a> */
<a name="line27"></a>
<a name="line28"></a>goog.provide(&#39;goog.crypt.Sha2&#39;);
<a name="line29"></a>
<a name="line30"></a>goog.require(&#39;goog.array&#39;);
<a name="line31"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line32"></a>goog.require(&#39;goog.crypt.Hash&#39;);
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * SHA-2 cryptographic hash constructor.
<a name="line38"></a> * This constructor should not be used directly to create the object. Rather,
<a name="line39"></a> * one should use the constructor of the sub-classes.
<a name="line40"></a> * @constructor
<a name="line41"></a> * @extends {goog.crypt.Hash}
<a name="line42"></a> * @struct
<a name="line43"></a> */
<a name="line44"></a>goog.crypt.Sha2 = function() {
<a name="line45"></a>  goog.crypt.Sha2.base(this, &#39;constructor&#39;);
<a name="line46"></a>
<a name="line47"></a>  this.blockSize = 512 / 8;
<a name="line48"></a>
<a name="line49"></a>  /**
<a name="line50"></a>   * A chunk holding the currently processed message bytes. Once the chunk has
<a name="line51"></a>   * 64 bytes, we feed it into computeChunk_ function and reset this.chunk_.
<a name="line52"></a>   * Sub-class needs to reset it when overriding reset().
<a name="line53"></a>   * @type {!Array.&lt;number&gt;}
<a name="line54"></a>   * @protected
<a name="line55"></a>   */
<a name="line56"></a>  this.chunk = [];
<a name="line57"></a>
<a name="line58"></a>  /**
<a name="line59"></a>   * Current number of bytes in this.chunk_.
<a name="line60"></a>   * Sub-class needs to reset it when overriding reset().
<a name="line61"></a>   * @type {number}
<a name="line62"></a>   * @protected
<a name="line63"></a>   */
<a name="line64"></a>  this.inChunk = 0;
<a name="line65"></a>
<a name="line66"></a>  /**
<a name="line67"></a>   * Total number of bytes in currently processed message.
<a name="line68"></a>   * Sub-class needs to reset it when overriding reset().
<a name="line69"></a>   * @type {number}
<a name="line70"></a>   * @protected
<a name="line71"></a>   */
<a name="line72"></a>  this.total = 0;
<a name="line73"></a>
<a name="line74"></a>  /**
<a name="line75"></a>   * Contains data needed to pad messages less than 64 bytes.
<a name="line76"></a>   * @type {!Array.&lt;number&gt;}
<a name="line77"></a>   * @private
<a name="line78"></a>   */
<a name="line79"></a>  this.pad_ = goog.array.repeat(0, this.blockSize);
<a name="line80"></a>  this.pad_[0] = 128;
<a name="line81"></a>
<a name="line82"></a>  /**
<a name="line83"></a>   * Holds the previous values of accumulated hash a-h in the computeChunk_
<a name="line84"></a>   * function.
<a name="line85"></a>   * It is a subclass-dependent value. Sub-class needs to explicitly set it
<a name="line86"></a>   * when overriding reset().
<a name="line87"></a>   * @type {!Array.&lt;number&gt;}
<a name="line88"></a>   * @protected
<a name="line89"></a>   */
<a name="line90"></a>  this.hash = [];
<a name="line91"></a>
<a name="line92"></a>  /**
<a name="line93"></a>   * The number of output hash blocks (each block is 4 bytes long).
<a name="line94"></a>   * It is a subclass-dependent value. Sub-class needs to explicitly set it
<a name="line95"></a>   * when overriding reset().
<a name="line96"></a>   * @type {number}
<a name="line97"></a>   * @protected
<a name="line98"></a>   */
<a name="line99"></a>  this.numHashBlocks = 0;
<a name="line100"></a>
<a name="line101"></a>  /**
<a name="line102"></a>   * Temporary array used in chunk computation.  Allocate here as a
<a name="line103"></a>   * member rather than as a local within computeChunk_() as a
<a name="line104"></a>   * performance optimization to reduce the number of allocations and
<a name="line105"></a>   * reduce garbage collection.
<a name="line106"></a>   * @type {!Array.&lt;number&gt;}
<a name="line107"></a>   * @private
<a name="line108"></a>   */
<a name="line109"></a>  this.w_ = [];
<a name="line110"></a>
<a name="line111"></a>  this.reset();
<a name="line112"></a>};
<a name="line113"></a>goog.inherits(goog.crypt.Sha2, goog.crypt.Hash);
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>/** @override */
<a name="line117"></a>goog.crypt.Sha2.prototype.reset = goog.abstractMethod;
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * Helper function to compute the hashes for a given 512-bit message chunk.
<a name="line122"></a> * @param {!Array.&lt;number&gt;} chunk A 512-bit message chunk to be processed.
<a name="line123"></a> * @private
<a name="line124"></a> */
<a name="line125"></a>goog.crypt.Sha2.prototype.computeChunk_ = function(chunk) {
<a name="line126"></a>  goog.asserts.assert(chunk.length == this.blockSize);
<a name="line127"></a>
<a name="line128"></a>  // Divide the chunk into 16 32-bit-words.
<a name="line129"></a>  var w = this.w_;
<a name="line130"></a>  var index = 0;
<a name="line131"></a>  var offset = 0;
<a name="line132"></a>  while (offset &lt; chunk.length) {
<a name="line133"></a>    w[index++] = (chunk[offset] &lt;&lt; 24) |
<a name="line134"></a>                 (chunk[offset + 1] &lt;&lt; 16) |
<a name="line135"></a>                 (chunk[offset + 2] &lt;&lt; 8) |
<a name="line136"></a>                 (chunk[offset + 3]);
<a name="line137"></a>    offset = index * 4;
<a name="line138"></a>  }
<a name="line139"></a>
<a name="line140"></a>  // Expand to 64 32-bit-words
<a name="line141"></a>  for (var i = 16; i &lt; this.blockSize; i++) {
<a name="line142"></a>    var s0 = ((w[i - 15] &gt;&gt;&gt; 7) | (w[i - 15] &lt;&lt; 25)) ^
<a name="line143"></a>             ((w[i - 15] &gt;&gt;&gt; 18) | (w[i - 15] &lt;&lt; 14)) ^
<a name="line144"></a>             (w[i - 15] &gt;&gt;&gt; 3);
<a name="line145"></a>    var s1 = ((w[i - 2] &gt;&gt;&gt; 17) | (w[i - 2] &lt;&lt; 15)) ^
<a name="line146"></a>             ((w[i - 2] &gt;&gt;&gt; 19) | (w[i - 2] &lt;&lt; 13)) ^
<a name="line147"></a>             (w[i - 2] &gt;&gt;&gt; 10);
<a name="line148"></a>    w[i] = (w[i - 16] + s0 + w[i - 7] + s1) &amp; 0xffffffff;
<a name="line149"></a>  }
<a name="line150"></a>
<a name="line151"></a>  var a = this.hash[0];
<a name="line152"></a>  var b = this.hash[1];
<a name="line153"></a>  var c = this.hash[2];
<a name="line154"></a>  var d = this.hash[3];
<a name="line155"></a>  var e = this.hash[4];
<a name="line156"></a>  var f = this.hash[5];
<a name="line157"></a>  var g = this.hash[6];
<a name="line158"></a>  var h = this.hash[7];
<a name="line159"></a>
<a name="line160"></a>  for (var i = 0; i &lt; this.blockSize; i++) {
<a name="line161"></a>    var S0 = ((a &gt;&gt;&gt; 2) | (a &lt;&lt; 30)) ^
<a name="line162"></a>             ((a &gt;&gt;&gt; 13) | (a &lt;&lt; 19)) ^
<a name="line163"></a>             ((a &gt;&gt;&gt; 22) | (a &lt;&lt; 10));
<a name="line164"></a>    var maj = ((a &amp; b) ^ (a &amp; c) ^ (b &amp; c));
<a name="line165"></a>    var t2 = (S0 + maj) &amp; 0xffffffff;
<a name="line166"></a>    var S1 = ((e &gt;&gt;&gt; 6) | (e &lt;&lt; 26)) ^
<a name="line167"></a>             ((e &gt;&gt;&gt; 11) | (e &lt;&lt; 21)) ^
<a name="line168"></a>             ((e &gt;&gt;&gt; 25) | (e &lt;&lt; 7));
<a name="line169"></a>    var ch = ((e &amp; f) ^ ((~ e) &amp; g));
<a name="line170"></a>    var t1 = (h + S1 + ch + goog.crypt.Sha2.K_[i] + w[i]) &amp; 0xffffffff;
<a name="line171"></a>
<a name="line172"></a>    h = g;
<a name="line173"></a>    g = f;
<a name="line174"></a>    f = e;
<a name="line175"></a>    e = (d + t1) &amp; 0xffffffff;
<a name="line176"></a>    d = c;
<a name="line177"></a>    c = b;
<a name="line178"></a>    b = a;
<a name="line179"></a>    a = (t1 + t2) &amp; 0xffffffff;
<a name="line180"></a>  }
<a name="line181"></a>
<a name="line182"></a>  this.hash[0] = (this.hash[0] + a) &amp; 0xffffffff;
<a name="line183"></a>  this.hash[1] = (this.hash[1] + b) &amp; 0xffffffff;
<a name="line184"></a>  this.hash[2] = (this.hash[2] + c) &amp; 0xffffffff;
<a name="line185"></a>  this.hash[3] = (this.hash[3] + d) &amp; 0xffffffff;
<a name="line186"></a>  this.hash[4] = (this.hash[4] + e) &amp; 0xffffffff;
<a name="line187"></a>  this.hash[5] = (this.hash[5] + f) &amp; 0xffffffff;
<a name="line188"></a>  this.hash[6] = (this.hash[6] + g) &amp; 0xffffffff;
<a name="line189"></a>  this.hash[7] = (this.hash[7] + h) &amp; 0xffffffff;
<a name="line190"></a>};
<a name="line191"></a>
<a name="line192"></a>
<a name="line193"></a>/** @override */
<a name="line194"></a>goog.crypt.Sha2.prototype.update = function(message, opt_length) {
<a name="line195"></a>  if (!goog.isDef(opt_length)) {
<a name="line196"></a>    opt_length = message.length;
<a name="line197"></a>  }
<a name="line198"></a>  // Process the message from left to right up to |opt_length| bytes.
<a name="line199"></a>  // When we get a 512-bit chunk, compute the hash of it and reset
<a name="line200"></a>  // this.chunk_. The message might not be multiple of 512 bits so we
<a name="line201"></a>  // might end up with a chunk that is less than 512 bits. We store
<a name="line202"></a>  // such partial chunk in this.chunk_ and it will be filled up later
<a name="line203"></a>  // in digest().
<a name="line204"></a>  var n = 0;
<a name="line205"></a>  var inChunk = this.inChunk;
<a name="line206"></a>
<a name="line207"></a>  // The input message could be either byte array of string.
<a name="line208"></a>  if (goog.isString(message)) {
<a name="line209"></a>    while (n &lt; opt_length) {
<a name="line210"></a>      this.chunk[inChunk++] = message.charCodeAt(n++);
<a name="line211"></a>      if (inChunk == this.blockSize) {
<a name="line212"></a>        this.computeChunk_(this.chunk);
<a name="line213"></a>        inChunk = 0;
<a name="line214"></a>      }
<a name="line215"></a>    }
<a name="line216"></a>  } else if (goog.isArray(message)) {
<a name="line217"></a>    while (n &lt; opt_length) {
<a name="line218"></a>      var b = message[n++];
<a name="line219"></a>      if (!(&#39;number&#39; == typeof b &amp;&amp; 0 &lt;= b &amp;&amp; 255 &gt;= b &amp;&amp; b == (b | 0))) {
<a name="line220"></a>        throw Error(&#39;message must be a byte array&#39;);
<a name="line221"></a>      }
<a name="line222"></a>      this.chunk[inChunk++] = b;
<a name="line223"></a>      if (inChunk == this.blockSize) {
<a name="line224"></a>        this.computeChunk_(this.chunk);
<a name="line225"></a>        inChunk = 0;
<a name="line226"></a>      }
<a name="line227"></a>    }
<a name="line228"></a>  } else {
<a name="line229"></a>    throw Error(&#39;message must be string or array&#39;);
<a name="line230"></a>  }
<a name="line231"></a>
<a name="line232"></a>  // Record the current bytes in chunk to support partial update.
<a name="line233"></a>  this.inChunk = inChunk;
<a name="line234"></a>
<a name="line235"></a>  // Record total message bytes we have processed so far.
<a name="line236"></a>  this.total += opt_length;
<a name="line237"></a>};
<a name="line238"></a>
<a name="line239"></a>
<a name="line240"></a>/** @override */
<a name="line241"></a>goog.crypt.Sha2.prototype.digest = function() {
<a name="line242"></a>  var digest = [];
<a name="line243"></a>  var totalBits = this.total * 8;
<a name="line244"></a>
<a name="line245"></a>  // Append pad 0x80 0x00*.
<a name="line246"></a>  if (this.inChunk &lt; 56) {
<a name="line247"></a>    this.update(this.pad_, 56 - this.inChunk);
<a name="line248"></a>  } else {
<a name="line249"></a>    this.update(this.pad_, this.blockSize - (this.inChunk - 56));
<a name="line250"></a>  }
<a name="line251"></a>
<a name="line252"></a>  // Append # bits in the 64-bit big-endian format.
<a name="line253"></a>  for (var i = 63; i &gt;= 56; i--) {
<a name="line254"></a>    this.chunk[i] = totalBits &amp; 255;
<a name="line255"></a>    totalBits /= 256; // Don&#39;t use bit-shifting here!
<a name="line256"></a>  }
<a name="line257"></a>  this.computeChunk_(this.chunk);
<a name="line258"></a>
<a name="line259"></a>  // Finally, output the result digest.
<a name="line260"></a>  var n = 0;
<a name="line261"></a>  for (var i = 0; i &lt; this.numHashBlocks; i++) {
<a name="line262"></a>    for (var j = 24; j &gt;= 0; j -= 8) {
<a name="line263"></a>      digest[n++] = ((this.hash[i] &gt;&gt; j) &amp; 255);
<a name="line264"></a>    }
<a name="line265"></a>  }
<a name="line266"></a>  return digest;
<a name="line267"></a>};
<a name="line268"></a>
<a name="line269"></a>
<a name="line270"></a>/**
<a name="line271"></a> * Constants used in SHA-2.
<a name="line272"></a> * @const
<a name="line273"></a> * @private {!Array.&lt;number&gt;}
<a name="line274"></a> */
<a name="line275"></a>goog.crypt.Sha2.K_ = [
<a name="line276"></a>  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
<a name="line277"></a>  0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
<a name="line278"></a>  0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
<a name="line279"></a>  0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
<a name="line280"></a>  0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
<a name="line281"></a>  0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
<a name="line282"></a>  0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
<a name="line283"></a>  0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
<a name="line284"></a>  0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
<a name="line285"></a>  0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
<a name="line286"></a>  0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
<a name="line287"></a>  0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
<a name="line288"></a>  0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
<a name="line289"></a>  0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
<a name="line290"></a>  0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
<a name="line291"></a>  0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
<a name="line292"></a>];
</pre>


</body>
</html>
